package org.dromara.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.mall.domain.MallOrders;
import org.dromara.mall.domain.vo.TurnoverVo;
import org.dromara.mall.mapper.MallOrdersMapper;
import org.dromara.mall.service.IMallOrdersService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
 * 全部订单Service业务层处理
 *
 * @author Mr Lei
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class MallOrdersServiceImpl implements IMallOrdersService {

    private final MallOrdersMapper baseMapper;

    /**
     * 查询全部订单
     */
    @Override
    public MallOrders queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询全部订单列表
     */
    @Override
    public TableDataInfo<MallOrders> queryPageList(MallOrders bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOrders> lqw = buildQueryWrapper(bo);
        Page<MallOrders> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询全部订单列表
     */
    @Override
    public List<MallOrders> queryList(MallOrders bo) {
        LambdaQueryWrapper<MallOrders> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<MallOrders> buildQueryWrapper(MallOrders bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOrders> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, MallOrders::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, MallOrders::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), MallOrders::getOrderNo, bo.getOrderNo());
        return lqw;
    }

    /**
     * 新增全部订单
     */
    @Override
    public Boolean insertByBo(MallOrders bo) {
        MallOrders add = MapstructUtils.convert(bo, MallOrders.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改全部订单
     */
    @Override
    public Boolean updateByBo(MallOrders bo) {
        MallOrders update = MapstructUtils.convert(bo, MallOrders.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOrders entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除全部订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /*根据日期营业额统计*/
    public TurnoverVo selectTenantByDate(LocalDate begin, LocalDate end) {
        //存放日期的数组
        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        //遍历指定的日期段
        while (!begin.equals(end)){
            //日期计算，以begin起end止，循环加1
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //查询每天的营业额
        ArrayList<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            //设置每天的开始和结束时间
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //创建一个存放的map对象
            HashMap<String,Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status",6);//订单状态-交易结束
            Double turnover = baseMapper.selectTurnoverByDate(map);
            turnover = turnover == null ? 0.00 : turnover; //如果当天值为0就有0.00替换
            turnoverList.add(turnover);
        }

        return TurnoverVo.builder()
            .dateList(StringUtils.join(dateList,','))
            .turnoverList(StringUtils.join(turnoverList,','))
            .build();
    }
}
